import Foundation

// 1.连接字符串
let str1 = "Hello"
let str2 = "World"
let concatenatedString = str1 + " " + str2
print(concatenatedString)  // 输出：Hello World

// 2.插入和追加
var str3 = "Hello"
str3.append("!")
print(str3)  // 输出：Hello!

var str4 = "Hello"
str4.insert("!", at: str4.endIndex)
print(str4)  // 输出：Hello!

// 3.删除
// remove(at:)：删除指定位置的字符：
var str5 = "Hello"
str5.remove(at: str5.startIndex)
print(str5)  // 输出：ello

// removeSubrange(_:)：删除一个范围的字符：
var str6 = "Hello"
str6.removeSubrange(str6.startIndex..<str6.index(str6.startIndex, offsetBy: 2))
print(str6)  // 输出：llo

// 4.比较字符串
let str7 = "Hello"
let str8 = "World"
print(str7 == str8)  // 输出：false
print(str7.compare(str8, options: .caseInsensitive) == .orderedSame)  // 忽略大小写比较

// 5.搜索和替换
// contains(_:)：检查是否包含子串：
let str9 = "Hello, World!"
print(str9.contains("World"))  // 输出：true

let str10 = "Hello, World!"
let newStr = str10.replacingOccurrences(of: "World", with: "Swift")
print(newStr)  // 输出：Hello, Swift!

// 6.大小写转换
let str11 = "Hello, World!"
print(str11.uppercased())  // 输出：HELLO, WORLD!
print(str11.lowercased())  // 输出：hello, world!
print(str11.capitalized)  // 输出：Hello, World!

// 访问字符
for char in "Hello" {
    print(char)
}

// 访问 Unicode 标量
let str12 = "Hello, 🌍!"
for scalar in str12.unicodeScalars {
    print(scalar)  // 直接打印，打印的是字符
    print(scalar.value)  // 输出 Unicode 标量值
}
